package com.leen.controller;

import com.alibaba.fastjson.JSONObject;
import com.leen.common.util.StringUtils;
import com.leen.common.vo.ResultEnum;
import com.leen.common.vo.ResultVO;
import com.leen.config.QiniuProperties;
import com.leen.service.QiniuService;
import com.leen.util.ImageUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.unit.DataSize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 文件上传
 *
 * @author admin
 * @date 2021-04-14
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping(value = "/leen")
public class UploadController {

    @Autowired
    private QiniuProperties  qiniuProperties;

    /**
     * 七牛云操作
     */
    @Autowired
    private QiniuService qiniuService;



    @PostMapping(value = "/image/upload")
    @ApiOperation(value = "单图片上传,只支持 jpg,png,gif文件")
    public ResultVO<String>  singleImageUpload(MultipartFile file) throws IOException {
        //文件有问题，或者没有传
        if(file.isEmpty()){
           return ResultVO.failed(ResultEnum.FILE_UPLOAD_NULL_ERROR);
        }

        //得到上传的文件名
        String fileName=file.getOriginalFilename();

        //做验证  jpg,png,gif
        if(StringUtils.isNotBlank(this.qiniuProperties.getImageExt())){
            boolean isAllowed=ImageUtil.allowImageType(fileName, this.qiniuProperties.getImageExt());
            if(!isAllowed){
                return ResultVO.failed(ResultEnum.FILE_FORMATTER_FAILD);
            }
        }

        //才能上传
        DefaultPutRet defaultPutRet = this.qiniuService.uploadFile(file);
        ResultVO  resultVO=ResultVO.success( defaultPutRet.key);

        return resultVO;
    }


    @PostMapping(value = "/image/thumb_upload")
    @ApiOperation(value = "单图片上传,只支持 jpg,png,gif文件")
    public ResultVO<String>  singleThumbImageUpload(MultipartFile file) throws IOException {
        //文件有问题，或者没有传
        if(file.isEmpty()){
            return ResultVO.failed(ResultEnum.FILE_UPLOAD_NULL_ERROR);
        }

        //得到上传的文件名
        String fileName=file.getOriginalFilename();

        //做验证  jpg,png,gif
        if(StringUtils.isNotBlank(this.qiniuProperties.getImageExt())){
            boolean isAllowed=ImageUtil.allowImageType(fileName, this.qiniuProperties.getImageExt());
            if(!isAllowed){
                return ResultVO.failed(ResultEnum.FILE_FORMATTER_FAILD);
            }
        }

        //才能上传
        Map<String, String> mp = this.qiniuService.uploadThumbFile(file);
        ResultVO  resultVO=ResultVO.success( mp);

        return resultVO;
    }


}
